#include <bits/stdc++.h>

#define rep(i, j, k) for (int i = (j); i <= (k); ++i)
#define per(i, j, k) for (int i = (j); i >= (k); --i)
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
using ll = long long;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;

template<class T>inline void chkmn(T &x, T y) { if (y < x) x = y; }
template<class T>inline void chkmx(T &x, T y) { if (y > x) x = y; }

using namespace std;

template <int P>
class mod_int {
  using Z = mod_int;

private:
  static int mo(int x) { return x < 0 ? x + P : x; }

public:
  int x;
  int val() const { return x; }
  mod_int() : x(0) {}
  template <class T>
  mod_int(const T &x_) : x(x_ >= 0 && x_ < P ? static_cast<int>(x_) : mo(static_cast<int>(x_ % P))) {}
  bool operator==(const Z &rhs) const { return x == rhs.x; }
  bool operator!=(const Z &rhs) const { return x != rhs.x; }
  Z operator-() const { return Z(x ? P - x : 0); }
  Z pow(long long k) const {
    Z res = 1, t = *this;
    while (k) {
      if (k & 1) res *= t;
      if (k >>= 1) t *= t;
    }
    return res;
  }
  Z &operator++() {
    x < P - 1 ? ++x : x = 0;
    return *this;
  }
  Z &operator--() {
    x ? --x : x = P - 1;
    return *this;
  }
  Z operator++(int) {
    Z ret = x;
    x < P - 1 ? ++x : x = 0;
    return ret;
  }
  Z operator--(int) {
    Z ret = x;
    x ? --x : x = P - 1;
    return ret;
  }
  Z inv() const { return pow(P - 2); }
  Z &operator+=(const Z &rhs) {
    (x += rhs.x) >= P && (x -= P);
    return *this;
  }
  Z &operator-=(const Z &rhs) {
    (x -= rhs.x) < 0 && (x += P);
    return *this;
  }
  Z &operator*=(const Z &rhs) {
    x = 1ULL * x * rhs.x % P;
    return *this;
  }
  Z &operator/=(const Z &rhs) { return *this *= rhs.inv(); }
#define setO(T, o)                                 \
  friend T operator o(const Z &lhs, const Z &rhs) {\
    Z res = lhs;                                   \
    return res o## = rhs;                          \
  }
  setO(Z, +) setO(Z, -) setO(Z, *) setO(Z, /)
#undef setO
};
const int P = 1000000007;
using Z = mod_int<P>;

const int maxn = 2010;

int n, a[maxn], b[maxn];
Z f[maxn][maxn], fac[maxn], ivf[maxn];

Z catalan(int n) {
  return fac[2 * n] * ivf[n] * ivf[n + 1];
}

int main() {
  freopen("zha.in", "r", stdin);
  freopen("zha.out", "w", stdout);
  fac[0] = 1;
  rep (i, 1, maxn - 1) fac[i] = fac[i - 1] * i;
  ivf[maxn - 1] = fac[maxn - 1].inv();
  per (i, maxn - 1, 1) ivf[i - 1] = ivf[i] * i;
  cin.tie(nullptr) -> ios::sync_with_stdio(false);
  cin >> n;
  rep (i, 1, n) cin >> a[i];
  rep (i, 1, n) cin >> b[i];
  f[1][1] = 1;
  rep (i, 1, n) rep (j, 1, n) {
    if (i == 1 && j == 1) continue;
    f[i][j] = f[i - 1][j] + f[i][j - 1];
    if (a[i] == b[j]) {
      int cnt = 0;
      rep (k, 1, min(i - 1, j - 1)) if (a[i - k] == b[j - k]) {
        f[i][j] -= f[i - k][j - k] * catalan(cnt), cnt++;
      }
    }
  }
  cout << f[n][n].val() << '\n';
}